/**
 * 
 */
package cn.nawang.ebeim.client.bean;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.nawang.ebeim.client.util.CommonMethodsUtils;
import cn.nawang.ebeim.client.util.Constants;
import cn.nawang.ebeim.client.util.DbUpDownloadFailedUtil;
import cn.nawang.ebeim.client.util.FileUtil;
import cn.nawang.ebeim.client.util.TransportUtil;
import cn.nawang.ebeim.common.entity.FileInfo;

/**
 * @ClassName: UpDownloadFailedBean
 * @Description: 上传下载失败的文件
 * @author zhangjf
 * @date 2015年11月30日 下午6:05:32
 * @version v1.0
 */
public class UpDownloadFailedBean {
  
  private static final Logger LOG = LoggerFactory.getLogger(UpDownloadFailedBean.class);

  private int id;
  private String dsId;
  private String signature;
  private String root;
  private String path;
  private String name;
  private String nid;
  private int status;
  private int type;
  private String recoveryPath;
  private long time;
  private int uid;
  private String isEncrypt;
  
  
  public void uploadTryAgain(){
    
    StorageServer server = TransportUtil.getPreferredServer();
    
    cn.nawang.ebeim.client.transfer.Client client =
        new cn.nawang.ebeim.client.transfer.Client(server.getHost(), server.getPort());
    
    String srcPath = getRoot() + File.separator + getPath();
    File file = new File(srcPath + File.separator + getName());
    if (!file.exists()) {
      LOG.info("file is not exist...:"+srcPath + File.separator + getName());
      DbUpDownloadFailedUtil.updateStatusById(DbUpDownloadFailedUtil.STATUS_ERROR, getId());
      return;
    }

    String dstPath = FileUtil.getUploadDataFilePath(getDsId(), getPath());
    File dFile = new File(dstPath);
    if (!dFile.exists()) {
      dFile.mkdirs();
    }
    
    if (getIsEncrypt().equals(Constants.IS_ENCRYPT_YES)) {
      /** 加密文件 */
      CommonMethodsUtils.encrypt(srcPath, dstPath, getName());
    } else {
      String fname = getName();
      /** 直接复制文件 */
      CommonMethodsUtils.copyFile(srcPath + File.separator + fname,
          dstPath + File.separator + fname);
    }
    
    FileInfo info = new FileInfo();
    info.setDataId(getDsId());
    info.setSignature(getSignature());
    info.setType(FileInfo.TYPE_FILE_UPLOAD);
    info.setNid(String.valueOf(getId()));
    
    String path = FileUtil.getUploadDataFilePath(getDsId(), getPath())+File.separator+getName();
    info.setPath(path);
    try {
      client.doTask(info);
      DbUpDownloadFailedUtil.updateStatusById(DbUpDownloadFailedUtil.STATUS_FINISHED, getId());
      File tmp = new File(path);
      if(tmp.exists()){
        tmp.delete();
      }
    } catch (Exception e) {
      DbUpDownloadFailedUtil.updateStatusById(DbUpDownloadFailedUtil.STATUS_ERROR, getId());
      e.printStackTrace();
    }
  }
  
  public void downloadTryAgain(){
    
    StorageServer server = TransportUtil.getPreferredServer();
    
    cn.nawang.ebeim.client.transfer.Client client =
        new cn.nawang.ebeim.client.transfer.Client(server.getHost(), server.getPort());
    FileInfo info = new FileInfo();
    info.setDataId(getDsId());
    info.setSignature(getSignature());
    info.setType(FileInfo.TYPE_FILE_DOWNLOAD);
    info.setNid(String.valueOf(getId()));
    info.setPath(getRoot()+getPath()+File.separator+getName());
    try {
      client.doTask(info);
      DbUpDownloadFailedUtil.updateStatusById(DbUpDownloadFailedUtil.STATUS_FINISHED, getId());
    } catch (Exception e) {
      DbUpDownloadFailedUtil.updateStatusById(DbUpDownloadFailedUtil.STATUS_ERROR, getId());
      e.printStackTrace();
      return;
    }
    
    String srcDir = FileUtil.getDownloadDataFilePath(getDsId());
    String srcPath = srcDir + File.separator + getSignature();
    String temp = srcDir + File.separator + getSignature() + ".tmp";
    
    if(getIsEncrypt().equals(Constants.IS_ENCRYPT_YES)){
      CommonMethodsUtils.decrypt(srcPath, temp);
    }else{
      File srcFile = new File(srcPath);
      File tempFile = new File(temp);
      srcFile.renameTo(tempFile);
    }
    
    String fileAbsolutePath = getPath() + File.separator + getName();
    String taskRecoveryPath = getRecoveryPath();
    String dest = taskRecoveryPath + fileAbsolutePath;
    /** 将已经还原的文件移动到用户自定义的位置中 */
    /** 设置重命名规则 */
    dest = CommonMethodsUtils.existFileRename(dest, null);
    CommonMethodsUtils.copyFile(temp, dest);

    File f1 = new File(srcPath);
    File f2 = new File(temp);
    if (f1.exists()) {
        f1.delete();
    }
    if (f2.exists()) {
        f2.delete();
    }
  }
  
  
  public String getIsEncrypt() {
    return isEncrypt;
  }
  public void setIsEncrypt(String isEncrypt) {
    this.isEncrypt = isEncrypt;
  }
  public int getUid() {
    return uid;
  }
  public void setUid(int uid) {
    this.uid = uid;
  }
  public String getRoot() {
    return root;
  }
  public void setRoot(String root) {
    this.root = root;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public long getTime() {
    return time;
  }
  public void setTime(long time) {
    this.time = time;
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getDsId() {
    return dsId;
  }
  public void setDsId(String dsId) {
    this.dsId = dsId;
  }
  public String getSignature() {
    return signature;
  }
  public void setSignature(String signature) {
    this.signature = signature;
  }
  public String getPath() {
    return path;
  }
  public void setPath(String path) {
    this.path = path;
  }
  public String getNid() {
    return nid;
  }
  public void setNid(String nid) {
    this.nid = nid;
  }
  public int getStatus() {
    return status;
  }
  public void setStatus(int status) {
    this.status = status;
  }
  public int getType() {
    return type;
  }
  public void setType(int type) {
    this.type = type;
  }
  public String getRecoveryPath() {
    return recoveryPath;
  }
  public void setRecoveryPath(String recoveryPath) {
    this.recoveryPath = recoveryPath;
  }
  @Override
  public String toString() {
    return "UpDownloadFailedBean [id=" + id + ", dsId=" + dsId + ", signature=" + signature
        + ", path=" + path + ", nid=" + nid + ", status=" + status + ", type=" + type
        + ", recoveryPath=" + recoveryPath + "]";
  }
  
}
